<?php
/**
 * 门店经纪人
 */

namespace app\badmin\controller;


use app\badmin\model\Company;
use app\badmin\model\CompanyAgent as CompanyAgentModel;
use app\badmin\model\CompanyShop as CompanyShopModel;
use app\common\controller\BadminController;
use think\Exception;
use think\facade\Request;

class CompanyAgent extends BadminController {

    public $resubmitLock = ['addpost', 'editpost','delpost'];

    /**
     * 添加获取
     * @return false|string
     */
    public function addGet() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            if ($params['account_type'] == 0) {//公司账号
                $map[] = ['company_id', '=', $params['company_id']];
                $map[] = ['deleted', '=', 0];
                $column = ['id,name'];
                $res1 = CompanyShopModel::getDatas($map, $column);//门店列表
                array_unshift($res1, ['id' => 0, 'name' => '直属公司']);
                $res2 = [];
            } else {//门店
                $Company = Company::getOne([['id', '=', $params['company_id']]], 'address');
                $CompanyShopModel = CompanyShopModel::getOne([['id', '=', $params['company_shop_id']]], 'address');
                $res1 = [];
                $res2 = [
                    ['address' => $Company['address']],
                    ['address' => $CompanyShopModel['address']]
                ];
            }
            return $this->resData('success', compact('res1', 'res2'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加提交
     * @return false|string
     */
    public function addPost() {
        try {
            $params = $this->checkParams('name,phone,position,address');
            if ($params === false) return $this->resErr();
            $res = CompanyAgentModel::post($params);
            if (!$res) return $this->resErr();
            else return $this->resData('创建成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 经纪人列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $res = CompanyAgentModel::getList($params);
            return $this->resData('success', $res);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑
     * @return false|string
     */
    public function editGet() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $res1 = CompanyAgentModel::getInfo($params);
            if (!$res1) return $this->resErr();
            if ($params['account_type'] == 0) {//公司账号
                $res2 = [];
            } else {
                $Company = Company::getOne([['id', '=', $params['company_id']]], 'address');
                $CompanyShopModel = CompanyShopModel::getOne([['id', '=', $params['company_shop_id']]], 'address');
                $res2 = [
                    ['address' => $Company['address']],
                    ['address' => $CompanyShopModel['address']]
                ];
            }
            if ($params['account_type'] == 0) {//公司账号
                $map[] = ['company_id', '=', $params['company_id']];
                $map[] = ['deleted', '=', 0];
                $column = ['id,name'];
                $shopList = CompanyShopModel::getDatas($map, $column);//门店列表
                array_unshift($shopList, ['id' => 0, 'name' => '直属公司']);
            } else {
                $shopList = [];
            }
            return $this->resData('success', compact('res1', 'res2', 'shopList'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑
     * @return false|string
     */
    public function editPost() {
        try {
            $params = $this->checkParams('id,name,phone,position,address');
            if ($params === false) return $this->resErr();
            $res = CompanyAgentModel::post($params);
            if (!$res) return $this->resErr();
            else return $this->resData('编辑成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除获取
     * @return false|string
     */
    public function delGet() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $res = CompanyAgentModel::transfer($params);
            if (!$res) return $this->resErr();
            return $this->resData('success', $res);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除提交
     * @return false|string
     */
    public function delPost() {
        try {
            $params = $this->checkParams('id,new_id');
            if ($params === false) return $this->resErr();
            $res = CompanyAgentModel::del($params);
            if (!$res) return $this->resErr();
            return $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 门店/公司地址
     * @return false|string
     */
    public function getAddress() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            if ($params['id'] == 0) {
                $Company = Company::getOne([['id', '=', $params['company_id']]], 'address');
                $data = [
                    ['address' => $Company['address']]
                ];
            } else {
                $Company = Company::getOne([['id', '=', $params['company_id']]], 'address');
                $CompanyShopModel = CompanyShopModel::getOne([['id', '=', $params['id']]], 'address');
                $data = [
                    ['address' => $Company['address']],
                    ['address' => $CompanyShopModel['address']]
                ];
            }
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
